{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "9d6fb9d8",
   "metadata": {},
   "source": [
    "------------\n",
    "* 2022/11/21/week 14\n",
    "* Python学习记录\n",
    "* 记录人：wu-huichun\n",
    "-----------"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f1cdfbde",
   "metadata": {},
   "source": [
    "# 本周目标: \n",
    "\n",
    "> 1、函数的定义\n",
    "\n",
    "> 2、如何使用函数\n",
    "\n",
    "> 3、函数的返回值\n",
    "\n",
    "> 4、使用注释改进文档\n",
    "\n",
    "> 5、传递任意数量的实参\n",
    "\n",
    "> 6、函数与模块"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "63ff3ccc",
   "metadata": {},
   "source": [
    "## 一、函数的定义"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "6a2c58e1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello郑先生~\n"
     ]
    }
   ],
   "source": [
    "def greet_user(username):\n",
    "    \"\"\"对某人的问候语\"\"\"\n",
    "    print(\"Hello\" + username + \"~\")\n",
    "greet_user(\"郑先生\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "e027f19d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6\n"
     ]
    }
   ],
   "source": [
    "def f(x):\n",
    "    \"\"\"y=2x的函数计算\"\"\"\n",
    "    print(2*x)\n",
    "f(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "aa074293",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "http://www.nfu.edu.cn/tzgg/index0.htm\n",
      "http://www.nfu.edu.cn/tzgg/index1.htm\n",
      "http://www.nfu.edu.cn/tzgg/index2.htm\n",
      "http://www.nfu.edu.cn/tzgg/index3.htm\n",
      "http://www.nfu.edu.cn/tzgg/index4.htm\n",
      "http://www.nfu.edu.cn/tzgg/index5.htm\n",
      "http://www.nfu.edu.cn/tzgg/index6.htm\n",
      "http://www.nfu.edu.cn/tzgg/index7.htm\n",
      "http://www.nfu.edu.cn/tzgg/index8.htm\n",
      "http://www.nfu.edu.cn/tzgg/index9.htm\n"
     ]
    }
   ],
   "source": [
    "# 实践编程思维（计算思维）下，函数的应用：\n",
    "def nfu_news_url(类别,page):\n",
    "    for page in range(10):\n",
    "        print(\"http://www.nfu.edu.cn/\" + 类别 + \"/index\" + str(page) + \".htm\")\n",
    "nfu_news_url(\"tzgg\",10)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "79b462a6",
   "metadata": {},
   "source": [
    "## 二、如何使用函数（传递实参）\n",
    "\n",
    "> 1.positional argument(位置参数)\n",
    "\n",
    "> 2.keyword argument(关键字参数)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "f6db68ee",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 通过案例来思考位置参数和关键字参数\n",
    "def f(x,y):\n",
    "    print(x)\n",
    "    print(y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "3cb43c1e",
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "positional argument follows keyword argument (3059410298.py, line 1)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  Input \u001b[1;32mIn [10]\u001b[1;36m\u001b[0m\n\u001b[1;33m    f(x=3,1)\u001b[0m\n\u001b[1;37m           ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m positional argument follows keyword argument\n"
     ]
    }
   ],
   "source": [
    "f(x=3,1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "340deb4c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "1\n"
     ]
    }
   ],
   "source": [
    "f(3,1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "cbdcf058",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "1\n"
     ]
    }
   ],
   "source": [
    "f(x=3,y=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "3cf24d90",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "1\n"
     ]
    }
   ],
   "source": [
    "f(3,y=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "551ed298",
   "metadata": {},
   "outputs": [],
   "source": [
    "def f(x,y,z):\n",
    "    print(x)\n",
    "    print(y)\n",
    "    print(z)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "9bb65a2a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "2\n",
      "5\n"
     ]
    }
   ],
   "source": [
    "f(3,2,5)   # 根据上面的经验，这个应该是位置参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "beeef8ea",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "10\n",
      "7\n"
     ]
    }
   ],
   "source": [
    "f(y=10,x=3,z=7)   # 根据上面的经验，这个应该是关键字参数，关键字参数不考虑顺序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "af26d1ab",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "9\n",
      "10\n"
     ]
    }
   ],
   "source": [
    "f(3,z=10,y=9)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "e5555e6a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "10\n",
      "8\n"
     ]
    }
   ],
   "source": [
    "f(3,y=10,z=8)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "e4669f14",
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "positional argument follows keyword argument (3426075602.py, line 1)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  Input \u001b[1;32mIn [21]\u001b[1;36m\u001b[0m\n\u001b[1;33m    f(x=3,y=9,10)\u001b[0m\n\u001b[1;37m                ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m positional argument follows keyword argument\n"
     ]
    }
   ],
   "source": [
    "f(3,y=9,10)   # 关键字参数必须放在位置参数后面"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b9131a1c",
   "metadata": {},
   "source": [
    "> 3.默认参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "cadd3335",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "I have a hamster.\n",
      "Myhamster's name is Harry.\n",
      "\n",
      "I have a cat.\n",
      "Mycat's name is Python.\n",
      "\n",
      "I have a dog.\n",
      "Mydog's name is Jerry.\n"
     ]
    }
   ],
   "source": [
    "def describe_pet(pet_name,animal_type=\"dog\",):\n",
    "    \"\"\"显示宠物信息,如果你的宠物是dog，可以不选择填写animal_type\"\"\"\n",
    "    print(\"\\nI have a \" + animal_type + \".\")\n",
    "    print(\"My\" + animal_type + \"'s name is \"+pet_name.title() + \".\")\n",
    "\n",
    "describe_pet('harry','hamster')\n",
    "describe_pet('python','cat')\n",
    "describe_pet('Jerry')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "3403e3dc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "I have a dog.\n",
      "Mydog's name is Jerry.\n",
      "None\n"
     ]
    }
   ],
   "source": [
    "print(describe_pet('Jerry'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "f207559e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function print in module builtins:\n",
      "\n",
      "print(...)\n",
      "    print(value, ..., sep=' ', end='\\n', file=sys.stdout, flush=False)\n",
      "    \n",
      "    Prints the values to a stream, or to sys.stdout by default.\n",
      "    Optional keyword arguments:\n",
      "    file:  a file-like object (stream); defaults to the current sys.stdout.\n",
      "    sep:   string inserted between values, default a space.\n",
      "    end:   string appended after the last value, default a newline.\n",
      "    flush: whether to forcibly flush the stream.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(print)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "49d5e192",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello world 郑先生\n",
      "hello world hand on\n"
     ]
    }
   ],
   "source": [
    "print(\"hello\",\"world\",\"郑先生\")\n",
    "print(\"hello\",\"world\",\"hand on\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "2e5aa5b3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on function describe_pet in module __main__:\n",
      "\n",
      "describe_pet(pet_name, animal_type='dog')\n",
      "    显示宠物信息\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(describe_pet)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cddbbbc7",
   "metadata": {},
   "source": [
    "## 三、函数的返回值\n",
    "\n",
    "> 1.几乎所有编程语言，函数是要求必须要写返回值！\n",
    "\n",
    "> 2.Python作为高级语言，可以不写返回值，但是一般情况下也是要写的，不写的话默认返回 None\n",
    "\n",
    "> 3.*函数的返回值*：return\n",
    "\n",
    ">> * return可以返回过程运算的结果\n",
    "\n",
    ">> * return可以返回过程自定义结果（由程序开发人员/设计人员定制的）\n",
    "\n",
    "## 四、函数的优势：\n",
    "\n",
    "* 1.可重复使用\n",
    "\n",
    "* 2.具有强大的可扩展性\n",
    "\n",
    "* 3.具有可定制性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "c213e28b",
   "metadata": {},
   "outputs": [],
   "source": [
    "def f(x):\n",
    "    y =x + 2\n",
    "    return y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "6ce7adde",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result = f(2)\n",
    "result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5a78362f",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
